package com.gfm.story.service.story;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gfm.story.dao.*;
import com.gfm.story.entity.dict.Dictionary;
import com.gfm.story.entity.story.Book;
import com.gfm.story.entity.story.Category;
import com.gfm.story.entity.story.Chapter;
import com.gfm.story.entity.user.User;
import com.gfm.story.interceptor.UserContext;
import com.gfm.story.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.service.story
 * @Author: Administrator
 * @CreateTime: 2019-06-04 13:39
 * @Description: ${Description}
 */
@Service
public class ChapterService {
    @Resource
    private ChapterDao chapterDao;
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private BookDao bookDao;
    @Resource
    private ReadLogService readLogService;
    @Resource
    private UserService userService;
    @Resource
    private DictionaryDao dictionaryDao;



    public Map<String,Object> getChapterByNews(Map<String,Object> map,Integer page) {
        //1.查询最新更新的章节
        List<Chapter> list = chapterDao.getChapterByNews();
        List<Long> booklist = new ArrayList<Long>();
        for (Chapter chapter:list){
            booklist.add(chapter.getBookId());
        }
        List<Book> list1 = new ArrayList<Book>();
        if (!booklist.isEmpty()){
            map.put("ids", booklist);
            list1 = chapterDao.getNewBookPage(map);
            for (Book book:list1){
                if (book.getCategoryId()!=null){
                    //修改图书类型
                    Category category = new Category();
                    category.setId(book.getCategoryId());
                    Category category1 = categoryDao.selectOne(category);
                    book.setCategoryName(category1.getName());
                }else{
                    book.setCategoryName("其他小说");
                }
            }
        }
        int count = chapterDao.getNewBookCount(map);
        Map<String,Object> map1 = new HashMap<String, Object>();
        map1.put("total", (count+20-1)/20);
        map1.put("currentPage", page);
        map1.put("objs", list1);
        return map1;
    }

    public List<Chapter> getChapterByChapterId(Long chapterId) {
        return chapterDao.getChapterByChapterId(chapterId);
    }

    public Chapter getChapterByBookId(Long id) {
        List<Chapter> list = chapterDao.getChapterByBookId(id);
        if (!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }



    public Map<String,Object> getChapterListByBookId(Map<String,Object> map1,Integer page) {
        Dictionary dictionary = new Dictionary();
        dictionary.setDictCode("BookDomainAddress");
        dictionary = dictionaryDao.selectOne(dictionary);
        Map<String,Object> map = new HashMap<String, Object>();
        //1.获取所有章节
        List<Chapter> list = chapterDao.getChapterPage(map1);
        int count = chapterDao.getChapterCountByBookId(map1);
        Chapter chapter = new Chapter();
        //2.获取最新章节信息
        List<Chapter> newlist = chapterDao.getChapterNewPage(map1);
        if (!newlist.isEmpty()){
            chapter = newlist.get(0);
        }
        //3.根据图书id获取图书信息
        Book book1 = bookDao.selectBookById(Long.parseLong(map1.get("bookId").toString()),null);
        book1.setImgUrl(dictionary.getDisc()+book1.getImgUrl());
        //4.获取小说类型
        Category category= new Category();
        Category category1=new Category();
        if (book1.getCategoryId()==null){
            category1.setName("其他小说");
        }else{
            category.setId(book1.getCategoryId());
            category1 = category.selectById();

        }
        //5.增加阅读记录
        User user = UserContext.getUser();
        if (user!=null){
            user = userService.getUserBySession(user);
            if (user!=null){
                readLogService.addReadLog(user.getId(), Long.parseLong(map1.get("bookId").toString()));
            }
        }

        map.put("book", book1);
        map.put("objs",list);
        map.put("newChapter",chapter);
        map.put("total", (count+20-1)/20);
        map.put("currentPage", page);
        map.put("category",category1);

        return map;
    }

    public List<Chapter> selectList(EntityWrapper<Chapter> wrapper) {
        List<Chapter> list = chapterDao.selectList(wrapper);
        return list;
    }
}
